import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemUserApi } from '#/api';

import { z } from '@vben/common-ui';

import { getAllRoleList, getDeptList } from '#/api/system';
import { $t } from '#/locales';

export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      label: $t('system.user.id'),
      dependencies: {
        triggerFields: ['id'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('system.user.username'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'alias',
      label: $t('system.user.userName'),
    },
    {
      component: 'Input',
      fieldName: 'email',
      label: $t('system.user.email'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'phone',
      label: $t('system.user.phone'),
      rules: 'required',
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: getDeptList,
        class: 'w-full',
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        style: { width: '100%' },
        dropdownStyle: { maxHeight: '400px', overflow: 'auto' },
      },
      fieldName: 'dept_id',
      label: $t('system.user.dept'),
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getAllRoleList,
        class: 'w-full',
        labelField: 'name',
        valueField: 'id',
        mode: 'multiple',
        style: { width: '100%' },
        dropdownStyle: { maxHeight: '400px', overflow: 'auto' },
      },
      fieldName: 'role_ids',
      label: $t('system.user.roles'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('system.user.status'),
    },

    {
      component: 'Input',
      componentProps: {
        type: 'password',
        placeholder: '请输入密码',
      },
      fieldName: 'password',
      label: $t('system.user.password'),
      rules: 'required',
      dependencies: {
        triggerFields: ['id'],
        show: (values) => !values.id, // 只在新增时显示
      },
    },
    {
      component: 'Input',
      componentProps: {
        type: 'password',
        placeholder: '请确认密码',
      },
      fieldName: 'confirmPassword',
      label: $t('system.user.confirmPassword'),
      dependencies: {
        triggerFields: ['id', 'password'],
        show: (values) => !values.id, // 只在新增时显示
        rules: (values) => {
          const { password } = values;
          return z
            .string({ required_error: $t('authentication.passwordTip') })
            .min(1, { message: $t('authentication.passwordTip') })
            .refine((value) => value === password, {
              message: $t('authentication.confirmPasswordTip'),
            });
        },
      },
    },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('system.user.username'),
    },
    {
      component: 'Input',
      fieldName: 'alias',
      label: $t('system.user.userName'),
    },
    {
      component: 'Input',
      fieldName: 'phone',
      label: $t('system.user.phone'),
    },
  ];
}

export function useColumns<T = SystemUserApi.User>(
  onActionClick?: OnActionClickFn<T>,
  onStatusChange?: (newStatus: any, row: T) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'username',
      title: $t('system.user.username'),
      width: 120,
    },
    {
      field: 'alias',
      title: $t('system.user.userName'),
      width: 120,
    },
    {
      field: 'email',
      title: $t('system.user.email'),
      width: 180,
    },
    {
      field: 'dept',
      title: $t('system.user.dept'),
      width: 120,
    },
    {
      field: 'phone',
      title: $t('system.user.phone'),
      width: 130,
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: onStatusChange ? 'CellSwitch' : 'CellTag',
        options: [
          { color: 'success', label: $t('common.enabled'), value: 1 },
          { color: 'error', label: $t('common.disabled'), value: 0 },
        ],
      },
      field: 'status',
      title: $t('system.user.status'),
      width: 100,
    },

    {
      field: 'created_at',
      title: $t('system.user.createTime'),
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'username',
          nameTitle: $t('system.user.name'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          'edit',
          {
            code: 'resetPassword',
            text: $t('system.user.resetPassword'),
          },
          'delete',
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: $t('system.user.operation'),
      width: 180,
    },
  ];
}
